﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HelloLLLLL.Blog.Models;
using HelloLLLLL.Blog.DAL;
using System.Text;

namespace HelloLLLLL.Blog.html
{
    public partial class Detail : System.Web.UI.Page
    {
        public LLArticle Arti=null;
        public List<LLType> listType;
        public List<Pinglun> ListParent = null;
        DBHelper helper = new DBHelper();
        public StringBuilder Sb=new StringBuilder();
        public int TitleId;
        public List<LLArticle> ListRandom;
        public  List<LLArticle> ListSim;

        protected void Page_Load(object sender, EventArgs e)
        {
            int titleId = int.Parse(string.IsNullOrEmpty(Request.QueryString["Id"])?"1":Request.QueryString["Id"]);
            TitleId = titleId;
            listType = new ModelHandler<LLType>().FillModel(helper.GetDataTable("select * from LLArtType", null));  
            if (titleId < 1)
            {
                Response.Redirect("ArticleList.aspx");
            }

            List < LLArticle > list= new ModelHandler<LLArticle>().FillModel(helper.GetDataTable("select * from LLArticle where Id=" + titleId, null));
            if (list != null && list.Count > 0)
            { Arti = list[0];

                int readCount = Arti.ReadCount;
                readCount = readCount + 1;
                string sql = "update LLArticle set ReadCount=" + readCount + " where Id=" + Arti.Id;

                helper.GetNonQuery(sql);

            }
            else
            {
                Response.Redirect("ArticleList.aspx");
            }
            GetPL();

            GetRandom();
            GetSimilar();
        }

        private void GetPL()
        {
            List<Pinglun> listAllPL = new ModelHandler<Pinglun>().FillModel(helper.GetDataTable("select * from Pinglun where [ArtId]=" + TitleId+ "order by id desc", null));
            if (listAllPL != null)
            {
                ListParent = listAllPL.Where(p => p.ReplyId == 0).ToList();
            
           
                LoopGetPL(ListParent, listAllPL);

            }


        }
        private void LoopGetPL(List<Pinglun> list, List<Pinglun>  listAllPL)
        {
            foreach (var item in list)
            {
                item.ChildPL = listAllPL.Where(p => p.ReplyId == item.Id).ToList();

                LoopGetPL(item.ChildPL, listAllPL);
            }
        }
        
        public string LoopList(List<Pinglun> list,string parent,int parentId )
        {           
            if (list != null) { 
            string str = @"<div class='comment-child'><a name ='reply-@reply.Id' ></a><img src ='../res/images/Leo.jpg' alt = {0} /><div class='info'><span class='username'>{1}</span><span style = 'padding-right:0;margin-left:-5px;'> 回复 </span ><span class='username'>{2}</span><span>{3}</span></div><p class='info'><span class='time'>{4}</span><a href = 'javascript:;' class='btn-reply' data-targetname='{5}'  data-targetid='{6}'>回复</a></p></div>";

            

            for (int i = 0; i < list.Count; i++)
            {
                string ss = string.Format(str, list[i].Pubnisher, list[i].Pubnisher, parent, list[i].Content, list[i].CTime, list[i].Pubnisher, list[i].Id);

               Sb.Append(ss);

                LoopList(list[i].ChildPL, list[i].Pubnisher, list[i].Id);
            }
            }
            if (Sb.Length>0)
            {
                return Sb.ToString();
            }
            return "";
        }

        private void GetRandom()
        {
            string sql = @"	 select  [Id]
      ,[Title]
      ,[HomeImg]
      ,[Digest]
      ,[ArtTime]
      ,[LastUpdatetime]
      ,[Content]
      ,[ReadPwd]
      ,[Editor]
      ,[PresentCount]
      ,[CommentCount]
      ,[ReadCount]
      ,[IsTop] from ( select top 8 * ,newid() random  from LLArticle order by random)  as t";

            ListRandom = new ModelHandler<LLArticle>().FillModel(helper.GetDataTable(sql, null));
        }

        private void GetSimilar()
        {
            string sql = "  select typeid from ArtiAndType where artId=" + TitleId;

            var dt = helper.GetDataTable(sql, null);

            string ids = "";

            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ids += dt.Rows[i][0].ToString() + ",";
                }
                if(ids!="")
                ids = ids.Substring(0, ids.Length - 1);
            }

            string sql2 = " select  top 5 a.[Id] ,[Title],[HomeImg] ,[Digest] ,[ArtTime],[LastUpdatetime],[Content],[ReadPwd],[Editor] ,[PresentCount],[CommentCount] ,[ReadCount],[IsTop] from LLArticle a join ArtiAndType b on a.Id = b.ArtId where b.TypeId in ({0}) and a.Id != {1} ";

            if (ids == "")
                ids = "0";

            sql2 = string.Format(sql2, ids, TitleId);
            ListSim = new ModelHandler<LLArticle>().FillModel(helper.GetDataTable(sql2, null));


        }
    }
}